<template>
  <el-container class="home-container">
    <!-- 页面头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" height="60px" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logOut">退出</el-button></el-header
    >
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 页面侧边区域 -->
      <el-aside :width="isCollapse ? '64px': '200px' " >
        <!-- 侧边栏菜单区域 -->
        <div class="topButton" @click="topButton">|||</div>
        <el-menu unique-opened
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse="isCollapse"
          :router="true"
          :default-active='activePath'
        >
          <el-submenu
            :index="item.id + ''"
            v-for="(item, index) in menuList"
            :key="item.id"
          >
            <!-- 一级菜单 -->
            <template slot="title">
              <!-- 一级菜单图标 -->
              <i :class="icon[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item-group
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+subItem.path + ''" @click="saveNavStatus('/'+subItem.path)">
                {{subItem.authName}}
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 页面右面主题区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //左侧菜单数据
      menuList: [],
      icon: {
        '125': "el-icon-user-solid",
        '103': "el-icon-copy-document",
        '101': "el-icon-shopping-bag-1",
        '102': "el-icon-tickets",
        '145': "el-icon-edit",
      },
      //是否水平折叠
      isCollapse:false,
      //被激活链接地址
      activePath:''
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    logOut() {
      window.sessionStorage.clear();
      this.$router.push("/Login");
      this.$message.success("退出成功");
    },
    //获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error("this.meta.msg");
      this.menuList = res.data;
    },
    //左边菜单栏 一键收起功能实现
    topButton(){
        this.isCollapse = !this.isCollapse
    },
    //保存链接的激活状态
    saveNavStatus(activePath){
      window.sessionStorage.setItem('activePath',activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  font-size: 20px;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header span {
  margin-left: 15px;
}
.el-aside {
  background-color: #333744;
}
.topButton{
    background-color: #4a5064;
    color: white;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    cursor: pointer;
    letter-spacing:2px;
}
.el-main {
  background-color: #eaedf1;
}
</style>